/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ccxc.question.commons.utils;

import java.util.HashMap;
import org.json.JSONObject;

import com.baidu.aip.face.AipFace;


/**
 * <p>人脸识别</p>
 */
public class Sample {

    //APPID
    public static final String APP_ID = "16358144";
    //API_KEY
    public static final String API_KEY = "WwDcOaBYlsc52vepZMSnV9ub";
    //SECRET_KEY
    public static final String SECRET_KEY = "s4cXuQSpVG59jWl5iWedH2LUFo0mjTHq";
    //client  AipFace对象
    private static AipFace client = null;


    static {
        getAipFace();
    }

    /**
     * <p>获取 AipFace对象</p>
     *
     * @return
     * @Title: getAipFace
     * @version:V0.1
     * @return:AipFace
     */
    public static AipFace getAipFace() {
        client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }


    /**
     * <p>人脸注册</p>
     *
     * @param imageBase64
     * @return
     * @Title: faceRe
     * @version:V0.1
     * @return:String
     */
    public static String faceRe(String imageBase64) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", "user's info");
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "LOW");

        String imageType = "BASE64";
        String groupId = "01";
        String userId = IdAndTimeUtil.getQuestionId();

        // 人脸注册
        JSONObject res = client.addUser(imageBase64, imageType, groupId, userId, options);
        /**
         * 直接获取的值face_token
         */
        System.out.println(res.toString(2));
        JSONObject result = res.getJSONObject("result");
        String[] opedId = JSONObject.getNames(result);

        return (String) result.get(opedId[0]);

    }


    /**
     * <p>人脸对比</p>
     *
     * @param imageBase64
     * @return
     * @Title: searchFace
     * @version:V0.1
     * @return:String
     */
    public static String searchFace(String imageBase64) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("quality_control", "NORMAL");
        options.put("liveness_control", "NONE");

        String image = imageBase64;
        String imageType = "BASE64";
        String groupIdList = "01";
        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupIdList, options);
        return res.toString(2);
    }
    /**
     * <p>人脸检测</p>
     *
     * @param imageBase64
     * @return
     * @Title: searchFace
     * @version:V0.1
     * @return:String
     */
    public static String jiancheFace(String imageBase64) {
    	    // 传入可选参数调用接口
    	    HashMap<String, String> options = new HashMap<String, String>();
    	    options.put("face_field", "age,beauty");
    	    options.put("max_face_num", "1");
    	    options.put("face_type", "LIVE");
    	    options.put("liveness_control", "LOW");

    	    String image = imageBase64;
    	    String imageType = "BASE64";

    	    // 人脸检测
    	    JSONObject res = client.detect(image, imageType, options);
    	    return res.toString(2);
    	   //int beauty = res.getInt("beauty"); 
    	    //if (beauty < 10) {
			//	return false;
			// else {
			//	return true;
			//}
			
    	

    }
}
